package com.joysuch.wwyt.patrol.controller;



import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.exception.ApiBusinessException;
import com.joysuch.wwyt.patrol.entity.bean.PatrolCheckPlanBean;
import com.joysuch.wwyt.patrol.service.PatrolCheckPlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zr
 * @since 2022-02-28
 */
@RestController
@Api(tags = "智能巡检-检查计划")
@RequestMapping("/patrol/patrol-check-plan")
public class PatrolCheckPlanController {

    @Autowired
    private PatrolCheckPlanService patrolCheckPlanService;


    @PostMapping("/savePlan")
    @ApiOperation("保存巡检计划")
    @RequiresPermissions("patrol:patrol-check-plan:savePlan")
    public ResultBean savePlan(@RequestBody PatrolCheckPlanBean planBean){
        try {
            patrolCheckPlanService.savePlan(planBean);
            return ResultBean.defaultSuccessResult();
        } catch (ApiBusinessException e) {
            return ResultBean.fail(e.getCode(),e.getMessage());
        }
    }

    @PostMapping("/updatePlan")
    @ApiOperation("修改巡检计划")
    @RequiresPermissions("patrol:patrol-check-plan:updatePlan")
    public ResultBean updatePlan(@RequestBody PatrolCheckPlanBean planBean){
        try {
            patrolCheckPlanService.updatePlan(planBean);
            return ResultBean.defaultSuccessResult();
        } catch (ApiBusinessException e) {
            return ResultBean.fail(e.getCode(),e.getMessage());
        }
    }

    @GetMapping("/checkPlanPage")
    @ApiOperation("巡检计划分页")
    public ResultBean checkPlanPage(Integer page , Integer size , String nameOrCode , Long responsibleDeptId,String statusName){
        return patrolCheckPlanService.checkPlanPage(page+1,size,nameOrCode,responsibleDeptId,statusName);
    }

    @GetMapping("/planNameIdMapping")
    @ApiOperation("巡检计划列表")
    public ResultBean checkPlanList(String planName){
        return ResultBean.success(patrolCheckPlanService.getPlanNameIdMapping(planName));
    }

    @GetMapping("/getPlanDetailById")
    @ApiOperation("根据检查计划id获取检查计划详情")
    public ResultBean getPlanDetailById(Long id){
        return ResultBean.success(patrolCheckPlanService.getPlanDetailById(id));
    }

    @GetMapping("/closeOrOpenPlan")
    @ApiOperation("开启或者关闭检查计划")
    @RequiresPermissions("patrol:patrol-check-plan:closeOrOpenPlan")
    public ResultBean closeOrOpenPlan(Long id,String flag){
        return patrolCheckPlanService.closeOrOpenPlan(id,flag);
    }

    @GetMapping("/deletePlanById")
    @ApiOperation("根据计划id删除计划")
    @RequiresPermissions("patrol:patrol-check-plan:deletePlanById")
    public ResultBean deletePlanById(Long id){
        return patrolCheckPlanService.deletePlanById(id);
    }

    @GetMapping("/get-all-name")
    @ApiOperation("获取所有计划名称")
    public ResultBean getAllPlanName() {
        List<String> nameList = patrolCheckPlanService.getAllName();
        return ResultBean.success(nameList);
    }


    @GetMapping("/getName")
    @ApiOperation("根据名称查询相关计划名称")
    public ResultBean getAllPlanName(String planName) {
        List<String> nameList = patrolCheckPlanService.getAllByName(planName);
        return ResultBean.success(nameList);
    }

}

